from Common import *
from DictParamChecker import *
import DB

class Currency:
    def __init__(self, logger, global_data):
        self.db = DB.DB(logger)
        self.logger = logger
        self.global_data = global_data

    def __handleSpecialArgs(self, args):
        if 'show_delete' in args and args['show_delete'] == '1':
            args['show_delete'] = True
        else:
            args['show_delete'] = False
        if 'return_raw' in args and args['return_raw'] == '1':
            args['return_raw'] = True
        else:
            args['return_raw'] = False
        return args
    
    def test(self, args):
        return makeReturn(Error.SUCCESS, {'hello':'world', 'from':'currency'})

    def getAll(self, args):
        args = self.__handleSpecialArgs(args)
        return self.db.call('getAllCurrency', args)

    def add(self, args):
        checker = DictParamChecker()
        checker.addParam('nameme', 'str', True)
        checker.addParam('unit', 'str', True)
        checker.addParam('characterter', 'str', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('addCurrency', args)

    def edit(self, args):
        checker = DictParamChecker()
        checker.addParam('crc_id', 'int', True)
        checker.addParam('nameme', 'str', True)
        checker.addParam('unit', 'str', True)
        checker.addParam('characterter', 'str', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('editCurrency', args)

    def delete(self, args):
        checker = DictParamChecker()
        checker.addParam('crc_id', 'int', True)
        args = checker.check(args)
        if type(args) == str:
            return makeReturn(Error.ILLEGAL_ARGS, args)
        return self.db.call('deleteCurrency', args)
        